/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JPnlSecondPage.java
 *
 * Created on 01/08/2010, 14:01:18
 */
package systole.view.wizard.analysis;

import java.util.Iterator;
import java.util.List;
import org.jdesktop.application.Action;
import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;
import systole.domain.persons.Medic;
import systole.domain.persons.Patient;
import systole.exceptions.ExceptionDAO;
import systole.persistence.FacadeDB;
import systole.view.crud.medic.ControllerMedicEdition;
import systole.view.crud.medic.ControllerMedicView;
import systole.view.crud.patient.ControllerPatientEdition;
import systole.view.crud.patient.ControllerPatientView;
import systole.view.utils.ErrorDialog;

/**
 *
 * @author jmj
 */
public class JPnlSecondPage extends javax.swing.JPanel {

    /**
     *
     */
    private static final long serialVersionUID = -3848685267280574740L;

    /** Creates new form JPnlSecondPage */
    public JPnlSecondPage() {
        initComponents();
        this.loadPatient();
        this.loadMedics();
        AutoCompleteDecorator.decorate(jCboxPatients);
        AutoCompleteDecorator.decorate(jCboxMedics);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jCboxPatients = new javax.swing.JComboBox();
        jCboxMedics = new javax.swing.JComboBox();
        jBtnNewPatient = new javax.swing.JButton();
        jBtnSearchPatient = new javax.swing.JButton();
        jBtnAddMedic = new javax.swing.JButton();
        jBtnSearchMedic = new javax.swing.JButton();
        jLblPatients = new javax.swing.JLabel();
        jLblMedics = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();

        setName("Form"); // NOI18N
        setPreferredSize(new java.awt.Dimension(350, 200));

        jCboxPatients.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(systole.view.SystoleApp.class).getContext().getResourceMap(JPnlSecondPage.class);
        jCboxPatients.setToolTipText(resourceMap.getString("jCboxPatients.toolTipText")); // NOI18N
        jCboxPatients.setName("jCboxPatients"); // NOI18N

        jCboxMedics.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jCboxMedics.setToolTipText(resourceMap.getString("jCboxMedics.toolTipText")); // NOI18N
        jCboxMedics.setName("jCboxMedics"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(systole.view.SystoleApp.class).getContext().getActionMap(JPnlSecondPage.class, this);
        jBtnNewPatient.setAction(actionMap.get("addPatient")); // NOI18N
        jBtnNewPatient.setIcon(resourceMap.getIcon("jBtnNewPatient.icon")); // NOI18N
        jBtnNewPatient.setToolTipText(resourceMap.getString("jBtnNewPatient.toolTipText")); // NOI18N
        jBtnNewPatient.setName("jBtnNewPatient"); // NOI18N
        jBtnNewPatient.setPreferredSize(new java.awt.Dimension(28, 28));

        jBtnSearchPatient.setAction(actionMap.get("searchPatient")); // NOI18N
        jBtnSearchPatient.setIcon(resourceMap.getIcon("jBtnSearchPatient.icon")); // NOI18N
        jBtnSearchPatient.setText(resourceMap.getString("jBtnSearchPatient.text")); // NOI18N
        jBtnSearchPatient.setToolTipText(resourceMap.getString("jBtnSearchPatient.toolTipText")); // NOI18N
        jBtnSearchPatient.setMaximumSize(new java.awt.Dimension(28, 28));
        jBtnSearchPatient.setMinimumSize(new java.awt.Dimension(28, 28));
        jBtnSearchPatient.setName("jBtnSearchPatient"); // NOI18N
        jBtnSearchPatient.setPreferredSize(new java.awt.Dimension(35, 35));

        jBtnAddMedic.setAction(actionMap.get("addMedic")); // NOI18N
        jBtnAddMedic.setIcon(resourceMap.getIcon("jBtnAddMedic.icon")); // NOI18N
        jBtnAddMedic.setText(resourceMap.getString("jBtnAddMedic.text")); // NOI18N
        jBtnAddMedic.setToolTipText(resourceMap.getString("jBtnAddMedic.toolTipText")); // NOI18N
        jBtnAddMedic.setMaximumSize(new java.awt.Dimension(28, 28));
        jBtnAddMedic.setMinimumSize(new java.awt.Dimension(28, 28));
        jBtnAddMedic.setName("jBtnAddMedic"); // NOI18N
        jBtnAddMedic.setPreferredSize(new java.awt.Dimension(35, 35));

        jBtnSearchMedic.setAction(actionMap.get("searchMedic")); // NOI18N
        jBtnSearchMedic.setIcon(resourceMap.getIcon("jBtnSearchMedic.icon")); // NOI18N
        jBtnSearchMedic.setText(resourceMap.getString("jBtnSearchMedic.text")); // NOI18N
        jBtnSearchMedic.setToolTipText(resourceMap.getString("jBtnSearchMedic.toolTipText")); // NOI18N
        jBtnSearchMedic.setMaximumSize(new java.awt.Dimension(28, 28));
        jBtnSearchMedic.setMinimumSize(new java.awt.Dimension(28, 28));
        jBtnSearchMedic.setName("jBtnSearchMedic"); // NOI18N
        jBtnSearchMedic.setPreferredSize(new java.awt.Dimension(35, 35));

        jLblPatients.setText(resourceMap.getString("jLblPatients.text")); // NOI18N
        jLblPatients.setName("jLblPatients"); // NOI18N

        jLblMedics.setText(resourceMap.getString("jLblMedics.text")); // NOI18N
        jLblMedics.setName("jLblMedics"); // NOI18N

        jLabel1.setFont(jLabel1.getFont().deriveFont(jLabel1.getFont().getStyle() | java.awt.Font.BOLD, jLabel1.getFont().getSize()+2));
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jCboxMedics, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jCboxPatients, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jBtnSearchPatient, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jBtnNewPatient, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jBtnSearchMedic, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jBtnAddMedic, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(jLblMedics))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(jLblPatients)))
                .addContainerGap(58, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jBtnAddMedic, jBtnNewPatient, jBtnSearchMedic, jBtnSearchPatient});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jLblPatients)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jBtnNewPatient, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jCboxPatients, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jBtnSearchPatient, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addComponent(jLblMedics)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jCboxMedics, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jBtnAddMedic, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jBtnSearchMedic, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(57, 57, 57))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jBtnAddMedic, jBtnNewPatient, jBtnSearchMedic, jBtnSearchPatient});

    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtnAddMedic;
    private javax.swing.JButton jBtnNewPatient;
    private javax.swing.JButton jBtnSearchMedic;
    private javax.swing.JButton jBtnSearchPatient;
    private javax.swing.JComboBox jCboxMedics;
    private javax.swing.JComboBox jCboxPatients;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLblMedics;
    private javax.swing.JLabel jLblPatients;
    // End of variables declaration//GEN-END:variables

    /**
     * Action to add a new patient
     */
    @Action
    public void addPatient() {
        ControllerPatientEdition controllerPatientEdition = new ControllerPatientEdition();
        controllerPatientEdition.newEntity();
        this.loadPatient();
    }

    /**
     * Action to add a new medic
     */
    @Action
    public void addMedic() {
        ControllerMedicEdition controllerMedicEdition = new ControllerMedicEdition();
        controllerMedicEdition.newEntity();
        this.loadMedics();
    }

    private void loadPatient() {
        try {
            this.jCboxPatients.removeAllItems();
            List<Patient> patients = FacadeDB.getInstance().getPatientBroker().getAllPatient();
            Iterator<Patient> itP = patients.iterator();
            while (itP.hasNext()) {
                this.jCboxPatients.addItem(itP.next());
            }
        } catch (ExceptionDAO ex) {
            FacadeDB.getInstance().refreshSession();
            ErrorDialog.showError(this, ex.getMessage());
        }
    }

    private void loadMedics() {
        try {
            this.jCboxMedics.removeAllItems();
            List<Medic> medics = FacadeDB.getInstance().getMedicBroker().getAllMedics();
            Iterator<Medic> it = medics.iterator();
            while (it.hasNext()) {
                this.jCboxMedics.addItem(it.next());
            }
        } catch (ExceptionDAO ex) {
            FacadeDB.getInstance().refreshSession();
            ErrorDialog.showError(this, ex.getMessage());
        }
    }

    /**
     * 
     */
    @Action
    public void searchPatient() {
        try {
            ControllerPatientView controllerPatientView = new ControllerPatientView();
            controllerPatientView.showFormToSelect(null);
            if (controllerPatientView.getSelectedEntity() != null) {
                this.jCboxPatients.setSelectedItem(controllerPatientView.getSelectedEntity());
                this.jCboxPatients.revalidate();
            }
        } catch (ExceptionDAO ex) {
            FacadeDB.getInstance().refreshSession();
            ErrorDialog.showError(this, ex.getMessage());
        }
    }

    /**
     * 
     */
    @Action
    public void searchMedic() {
        try {
            ControllerMedicView controllerMedicView = new ControllerMedicView();
            controllerMedicView.showFormToSelect(null);
            if (controllerMedicView.getSelectedEntity() != null) {
                this.jCboxMedics.setSelectedItem(controllerMedicView.getSelectedEntity());
                this.jCboxMedics.revalidate();
            }
        } catch (ExceptionDAO ex) {
            FacadeDB.getInstance().refreshSession();
            ErrorDialog.showError(this, ex.getMessage());
        }
    }

    /**
     * @return a string wit error message
     */
    public String validateConditions() {

        if (this.jCboxPatients.getSelectedItem() == null) {
            return "Debe seleccionar un Paciente";
        }
        if (this.jCboxMedics.getSelectedItem() == null) {
            return "Debe seleccionar un Médico";
        }
        return null;
    }

    /**
     * @return medic
     */
    public Medic getMedicSelected() {
        return (Medic) this.jCboxMedics.getSelectedItem();
    }

    /**
     * @return patient
     */
    public Patient getPatientSelected() {
        return (Patient) this.jCboxPatients.getSelectedItem();
    }
}
